import axios from 'axios'

axios.interceptors.request.use((config) => {
  return config
}, (error) => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  return response
}, error => {
  return Promise.reject(error.response.data)
})

export default function fetch (url:string, param:any, method:string) {
  const config = {
    method: method || 'GET',
    url: url,
    params: null,
    data: null
  }
  if (config.method === 'GET') {
    config.params = param
  } else {
    config.data = param
  }

  return new Promise((resolve, reject) => {
    axios(config)
    .then(response => {
      resolve(response.data)
    }, err => {
      resolve(err)
    })
    .catch((error) => {
      reject(error)
    })
  })
}